package com.wzy.mall.service.admin.impl;

import com.wzy.mall.mapper.adminMapper.GoodsMapper;
import com.wzy.mall.model.bo.adminBO.*;
import com.wzy.mall.model.pojo.adminPOJO.GoodsPOJO;
import com.wzy.mall.model.pojo.adminPOJO.SpecDeletePOJO;
import com.wzy.mall.model.pojo.adminPOJO.SpecPOJO;
import com.wzy.mall.model.pojo.adminPOJO.TypePOJO;
import com.wzy.mall.model.vo.adminVO.*;
import com.wzy.mall.service.admin.GoodsService;
import com.wzy.mall.utils.HTTPUtils;
import com.wzy.mall.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.*;

public class GoodsServiceImpl implements GoodsService {

    public List<TypePOJO> getType() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        List<TypePOJO> typePOJOList = null;
        try {
            typePOJOList = mapper.getType();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return typePOJOList;
    }

    public List<GoodsPOJO> getGoodsByType(int typeId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        List<GoodsPOJO> goodsPOJOList = null;
        try {
            String imageBaseURL = HTTPUtils.getImageBaseURL();
            goodsPOJOList = mapper.getGoodsByType(typeId);
            for (GoodsPOJO goodsPOJO : goodsPOJOList) {
                String img = goodsPOJO.getImg();
                img = imageBaseURL + img;
                //System.out.println(img);
                goodsPOJO.setImg(img);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return goodsPOJOList;
    }



    public int addType(String typeName) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        int result = 0;
        try {
            result = mapper.addType(typeName);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return result;
    }

    public int deleteType(int typeId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        int result = 0;
        try {
            // 删除类目前，根据typeID将商品的visible属性置为false
            result = mapper.updateGoodsVisible(typeId);

            result = mapper.deleteType(typeId);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return result;
    }

    public GoodsInfoVO getGoodsInfo(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        // 获取商品基本信息
        GoodsPOJO goodsPOJO = mapper.getGoodsByID(id);
        // 获取商品规格信息
        List<SpecPOJO> specPOJOList = mapper.getSpecByGoodsID(id);

        // 将两个信息封装进VO对象
        GoodsInfoVO goodsInfoVO = new GoodsInfoVO();
        GoodsBaseVO goodsBaseVO = new GoodsBaseVO();
        ArrayList<SpecVO> specVOList = new ArrayList<SpecVO>();

        // 获取图片路径前缀
        try {
            String imageBaseURL = HTTPUtils.getImageBaseURL();
            // 将商品基本信息封装进VO对象
            goodsBaseVO.setId(goodsPOJO.getId());
            goodsBaseVO.setName(goodsPOJO.getGoodsname());
            goodsBaseVO.setImg(imageBaseURL + goodsPOJO.getImg());
            goodsBaseVO.setDesc(goodsPOJO.getDescription());
            goodsBaseVO.setUntiPrice(goodsPOJO.getPrice());
            goodsBaseVO.setTypeId(goodsPOJO.getType_id());

            // 将规格信息遍历封装进VO集合
            for (SpecPOJO specPOJO : specPOJOList) {
                SpecVO specVO = new SpecVO();
                specVO.setId(specPOJO.getId());
                specVO.setSpecName(specPOJO.getSpec_name());
                specVO.setStockNum(specPOJO.getStocknum());
                specVO.setUnitPrice(specPOJO.getUnitprice());

                specVOList.add(specVO);
            }

            // 将两个信息封装进GoodInfoVo对象
            goodsInfoVO.setGoods(goodsBaseVO);
            goodsInfoVO.setSpecs(specVOList);

            //System.out.println(goodsInfoVO);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return goodsInfoVO;
    }

    public int deleteSpec(SpecDeleteBO specDeleteBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        SpecDeletePOJO specDeletePOJO = new SpecDeletePOJO();
        specDeletePOJO.setGoods_id(specDeleteBO.getGoodsId());
        specDeletePOJO.setSpec_name(specDeleteBO.getSpecName());

        int result = 0;
        try {
            result = mapper.deleteSpec(specDeletePOJO);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return result;
    }

    public int addSpec(SpecAddBO specAddBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        SpecPOJO specPOJO = new SpecPOJO();
        specPOJO.setSpec_name(specAddBO.getSpecName());
        specPOJO.setGoods_id(specAddBO.getGoodsId());
        specPOJO.setStocknum(specAddBO.getStockNum());
        specPOJO.setUnitprice(specAddBO.getUnitPrice());

        // 先查找对应商品ID下是否存在相同的规格名称
        int result = mapper.selectSpecByGoodsID(specPOJO);
        if(result != 0) {
            // 返回505表示已存在对应商品ID下的规格名称
            return 505;
        }

        try {
            result = mapper.addSpec(specPOJO);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        if(result == 0) {
            return 404;
        } else {
            //System.out.println("回传主键" + specPOJO.getId());
            return 200;
        }
    }

    public int deleteGoodsByID(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        int result = 0;
        try {
            result = mapper.deleteGoodsByID(id);
            // 需要删除商品的同时删除规格表内的相关数据
            result = mapper.deleteSpecByGoodsID(id);

            // DO 需要删除对应商品的留言和评价
            if (result != 0) {
                mapper.deleteReplyByGoodsID(id);
                mapper.deleteCommentsByGoodsID(id);
            }

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            sqlSession.close();
        }
        return result;
    }

    public int updateGoodsByID(GoodsUpdateBO goodsUpdateBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        GoodsPOJO goodsPOJO = new GoodsPOJO();
        goodsPOJO.setId(goodsUpdateBO.getId());
        String img = goodsUpdateBO.getImg();
        // 以防万一，将图片地址的前缀去掉
        try {
            img = img.replaceAll(HTTPUtils.getImageBaseURL(), "");
        } catch (IOException e) {
            e.printStackTrace();
        }
        goodsPOJO.setImg(img);
        goodsPOJO.setGoodsname(goodsUpdateBO.getName());
        goodsPOJO.setDescription(goodsUpdateBO.getDesc());
        goodsPOJO.setType_id(goodsUpdateBO.getTypeId());

        // 最小单价及其存货量
        Double minUnitPrice = null;
        Integer minUPStockNum = null;

        for (SpecUpdateBO spec : goodsUpdateBO.getSpecList()) {
            if(minUnitPrice == null) {
                minUnitPrice = spec.getUnitPrice();
                minUPStockNum = spec.getStockNum();
            }
            if(minUnitPrice > spec.getUnitPrice()) {
                minUnitPrice = spec.getUnitPrice();
                minUPStockNum = spec.getStockNum();
            }
        }
        goodsPOJO.setPrice(minUnitPrice);
        goodsPOJO.setStocknum(minUPStockNum);

        int result = 0;

        // 更改商品信息同时更改规格表
        //List<SpecPOJO> specPOJOList = new ArrayList<SpecPOJO>();
        List<SpecUpdateBO> specUpdateBOList = goodsUpdateBO.getSpecList();
        try {
            for (SpecUpdateBO specUpdateBO : specUpdateBOList) {
                //System.out.println(specUpdateBO);

                SpecPOJO specPOJO = new SpecPOJO();
                specPOJO.setId(specUpdateBO.getId());
                specPOJO.setSpec_name(specUpdateBO.getSpecName());
                specPOJO.setGoods_id(specUpdateBO.getGoodsId());
                specPOJO.setStocknum(specUpdateBO.getStockNum());
                specPOJO.setUnitprice(specUpdateBO.getUnitPrice());

                if(specPOJO.getId() != null) {
                    result = mapper.updateSpec(specPOJO);
                    if(result == 0) {
                        return result;
                    }
                } else {
                    result = mapper.addSpec(specPOJO);
                    if(result == 0) {
                        return result;
                    }
                }
            }

            result = mapper.updateGoodsByID(goodsPOJO);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {

            sqlSession.close();
        }
        return result;
    }

    public int addGoods(GoodsAddBO goodsAddBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        // 将商品的基本信息封装进POJO对象
        GoodsPOJO goodsPOJO = new GoodsPOJO();
        goodsPOJO.setGoodsname(goodsAddBO.getName());
        goodsPOJO.setImg(goodsAddBO.getImg());
        goodsPOJO.setDescription(goodsAddBO.getDesc());
        goodsPOJO.setType_id(goodsAddBO.getTypeId());

        int result = 0;
        // 根据TypeID和商品名查找是否有同名商品
        result = mapper.selectGoodsByName(goodsPOJO);
        if(result == 1) {
            // 存在同名商品，返回505
            return 505;
        }

        // 最小单价及其存货量
        Double minUnitPrice = null;
        Integer minUPStockNum = null;

        for (SpecAddBO spec : goodsAddBO.getSpecList()) {
            if(minUnitPrice == null) {
                minUnitPrice = spec.getUnitPrice();
                minUPStockNum = spec.getStockNum();
            }
            if(minUnitPrice > spec.getUnitPrice()) {
                minUnitPrice = spec.getUnitPrice();
                minUPStockNum = spec.getStockNum();
            }
        }
        goodsPOJO.setPrice(minUnitPrice);
        goodsPOJO.setStocknum(minUPStockNum);


        // DO 将商品基本信息插入数据库 获取回传主键
        try {
            result = mapper.addGoodsInfo(goodsPOJO);
            //System.out.println("回传主键："+goodsPOJO.getId());
            //int i = 1/0;

            // 获取每个规格的单价和存货量
            List<SpecPOJO> specPOJOList = new ArrayList<SpecPOJO>();
            for (SpecAddBO spec : goodsAddBO.getSpecList()) {
                SpecPOJO specPOJO = new SpecPOJO();
                specPOJO.setSpec_name(spec.getSpecName());
                specPOJO.setUnitprice(spec.getUnitPrice());
                specPOJO.setStocknum(spec.getStockNum());
                // 将回传主键赋值给规格的商品ID
                specPOJO.setGoods_id(goodsPOJO.getId());

                specPOJOList.add(specPOJO);
            }

            result = mapper.addSpecList(specPOJOList);

            sqlSession.commit();
        } catch (Exception e) {
            return 0;
        } finally {
            sqlSession.close();
        }

        return result;
    }

    public List<ReplyNVO> noReplyMsg() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        List<ReplyNVO> replyNVOList = null;
        try {
            replyNVOList = mapper.selectNoReplyMsg();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return replyNVOList;
    }

    public List<RepliedYVO> repliedMsg() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        List<RepliedYVO> repliedYVOList = null;
        try {
            repliedYVOList = mapper.selectRepliedMsg();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return repliedYVOList;
    }

    public int reply(ReplyBO replyBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        int result = 0;
        try {
            // 需要更新回复留言和状态码还有回复时间！！
            result = mapper.updateReply(replyBO);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return result;
    }
}
